package com.sghj.SysPermission.controller.front;//package com.zjcq.SysPermission.controller.front;
//
//import com.alibaba.fastjson2.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.zjcq.SysPermission.entity.DTO.EmployeeModificationMiniProgramPerviewDTO;
//import com.zjcq.SysPermission.entity.DTO.PhoneMiniProgramRoleDTO;
//import com.zjcq.SysPermission.entity.SysUser;
//import com.zjcq.SysPermission.entity.Vo.MiniProgramRoleIdListVO;
//import com.zjcq.SysPermission.entity.Vo.userLoginVo;
//import com.zjcq.SysPermission.entity.front.InfoEmployee;
//import com.zjcq.SysPermission.entity.front.Vo.employeeCheckVo;
//import com.zjcq.SysPermission.entity.front.Vo.employeeShowInfo;
//import com.zjcq.SysPermission.service.InfoEmployeeService;
//import com.zjcq.SysPermission.service.SysMiniProgramRoleService;
//import com.zjcq.config.Log;
//import com.zjcq.config.handler.ResultCode;
//import com.zjcq.config.handler.exceptionHandler.MyException;
//import com.zjcq.pageviews.service.SysAccessLogService;
//import com.zjcq.syslog.entity.SysEmployeeinfoChangeLog;
//import com.zjcq.syslog.service.SysEmployeeinfoChangeLogService;
//import com.zjcq.utils.JwtUtils;
//import com.zjcq.config.ReturnResult;
//import com.zjcq.utils.StringUtils;
//import com.zjcq.utils.UploadImgUtils;
//import com.zjcq.utils.accountStatus.accountStatus;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.aspectj.lang.annotation.AfterReturning;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.validation.constraints.Min;
//import javax.validation.constraints.NotNull;
//import java.util.List;
//
///**
// * @author xrx
// * @Date 2022/2/18
// * @apiNote
// */
//@RestController
//@RequestMapping("/user")
//@Validated
//public class frontUserController {
//
//    @Autowired
//    private InfoEmployeeService employeeService;
//    @Autowired
//    private SysAccessLogService accessLogService;
//    @Autowired
//    private SysEmployeeinfoChangeLogService employeeinfoChangeLogService;
//    @Autowired
//    private SysMiniProgramRoleService programRoleService;
//
//
//    /**
//     * 前端登录
//     * @param user
//     * @return
//     */
//    //@RateLimiter(count = 10,time = 10)
//    @PostMapping("frontLogin")
//    public ReturnResult login(@RequestBody userLoginVo user){
//        //if (user==null|| StringUtils.isEmpty(user.getUserName())||StringUtils.isEmpty(user.getPassWord())){
//        //    throw new MyException(ResultCode.PARAMETER,"请输入账号密码");
//        //}
//        //登录 ->生成token
//        String token =employeeService.login(user);
//
//        //if (token==null||token.equals("")){
//        //    return ReturnResult.failed().message("登录失败");
//        //}
//        accessLogService.insertLog(token);
//        return ReturnResult.success().message("登录成功").data("token",token);
//    }
//
//    /**
//     * 根据token获取用户信息
//     * @param request
//     * @return
//     */
//    @GetMapping("getUserInfo")
//    public ReturnResult getUserInfo(HttpServletRequest request){
//        //调用jwt工具类方法，根据request对象获取头信息，返回用户id
//        Integer usrid= JwtUtils.getMemberIdByJwtToken(request);
//        if (usrid==null||usrid.equals("")){
//            throw new MyException(ResultCode.GETTOKENERROR,"获取token信息失败");
//        }
//
//        //查询数据库，根据用户ID获取用户信息
//        //InfoEmployee employeeInfo = employeeService.getById(usrid);
//        employeeShowInfo employeeInfoFront = employeeService.getEmployeeInfoFront(usrid);
//
//        return ReturnResult.success().data("employeeInfo",employeeInfoFront);
//    }
//
//    /**
//     * 根据id获取用户信息
//     * @param id
//     * @return
//     */
//    @GetMapping("getuserinfo/{id}")
//    public ReturnResult getuser(@PathVariable String id){
//        InfoEmployee byId = employeeService.getById(id);
//
//        if (byId==null){
//            return ReturnResult.failed().message("没有查询到该用户数据");
//        }
//        return ReturnResult.success().data("user",byId);
//    }
//
//    /**
//     * 修改用户手机号
//     * @param employee
//     * @return
//     */
//    @PostMapping("updatePhone")
//    @Log("修改手机号")
//    public ReturnResult updatePhone(@Validated @RequestBody InfoEmployee employee){
//        if (employee==null||employee.getPhone().equals("")){
//            throw new MyException(ResultCode.PARAMETER,"请输入修改后的手机号码");
//        }
//        int row = employeeService.updatePhone(employee);
//
//        if (row>0){
//            return ReturnResult.success().message("修改成功").data("afterPhone",employee.getPhone());
//        }
//        return ReturnResult.failed().message("修改失败");
//    }
//
//    /**
//     * 修改密码
//     * @param employee
//     * @return
//     */
//
//    @PostMapping("updatePassword")
//    @Log("修改登录密码")
//    public ReturnResult updatePassword(@Validated @RequestBody InfoEmployee employee){
//        if (employee==null||employee.getPassWord().equals("")){
//            throw new MyException(ResultCode.PARAMETER,"请输入修改后的密码");
//        }
//        if (employeeService.getById(employee.getEmployeeId())==null){
//            throw new MyException(ResultCode.NOTQUERIED,"无该用户数据");
//        }
//
//        int row = employeeService.updatePassword(employee);
//
//        if (row>0){
//            return ReturnResult.success().message("修改成功");
//        }
//        return ReturnResult.failed().message("修改失败");
//    }
//
//    /**
//     * 查询员工详细信息（前端展示）
//     * @param eid
//     * @return
//     */
//    @GetMapping("getEmpInfoFront/{eid}")
//    public ReturnResult getEmpInfoFront(@PathVariable Integer eid){
//        if (eid==null||eid.equals("")){
//            throw new MyException(ResultCode.PARAMETER,"缺少参数");
//        }
//
//        employeeShowInfo employeeInfoFront = employeeService.getEmployeeInfoFront(eid);
//
//        //if (employeeInfoFront!=null){
//        //    return ReturnResult.success().message("查询成功").data("employeeInfo",employeeInfoFront);
//        //}
//        //return ReturnResult.failed().message("查询失败");
//        return ReturnResult.success().message("查询成功").data("employeeInfo",employeeInfoFront);
//    }
//
//    @RequiresPermissions("service:em:add")
//    @PostMapping("addEmployee")
//    @Log("添加员工信息")
//    //@AfterReturning()
//    public ReturnResult addEmployee(@Validated InfoEmployee newEmployee,@RequestParam(required = false) MultipartFile photo1){
//        if (newEmployee==null){
//            throw new MyException(ResultCode.PARAMETER,"缺少参数");
//        }
//
//        //添加员工
//        int row =employeeService.addEmployee(newEmployee,photo1);
//        if (row>0){
//            return ReturnResult.success().message("添加员工信息成功");
//        }
//        return ReturnResult.failed().message("添加员工信息失败");
//    }
//
//    /**
//     * 更新用户信息
//     * @return
//     */
//    @RequiresPermissions("service:em:update")
//    @PostMapping("updateEmployee")
//    @Log("修改用户信息")
//    public ReturnResult updateEmployee(InfoEmployee newEmployee,@RequestParam(required = false) MultipartFile photo1){
//        if (newEmployee==null||newEmployee.getEmployeeId()==null){
//            throw new MyException(ResultCode.PARAMETER,"缺少参数");
//        }
//        //当前人员信息
//        InfoEmployee oldEmp = employeeService.getById(newEmployee.getEmployeeId());
//
//        QueryWrapper<InfoEmployee> wrapper = new QueryWrapper<>();
//        wrapper.eq("Phone",newEmployee.getPhone());
//        wrapper.ne("Phone",oldEmp.getPhone());
//        if (employeeService.count(wrapper)>0){
//            throw new MyException(ResultCode.REPECT,"该手机号已被绑定");
//        }
//
//        //如果修改工作证号
//        if (StringUtils.isNotEmpty(newEmployee.getWorkCardId())) {
//            QueryWrapper<InfoEmployee> wrapper1 = new QueryWrapper<>();
//            wrapper1.eq("WorkCardId",newEmployee.getWorkCardId());
//            wrapper1.ne("WorkCardId",oldEmp.getWorkCardId());
//            if (employeeService.count(wrapper1)>0){
//                throw new MyException(ResultCode.REPECT,"该工作证号已存在");
//            }
//        }
//        newEmployee.setInstitutionRoleId(programRoleService.getInstitutionRoleIdByDeptId(newEmployee.getInstitutionId()));
//
//        boolean flag =false;
//        //如果照片更新
//        if (photo1!=null&&!photo1.isEmpty()) {
//            flag = employeeService.updateEmployee(newEmployee, photo1);
//        }else{
//            //照片不更新
//            if (StringUtils.isEmpty(newEmployee.getPhoto())){
//                newEmployee.setPhoto(UploadImgUtils.defaultPhoto);
//                //newEmployee.setPhotoBase64(UploadImgUtils.defaultPhoto);
//            }
//            flag= employeeService.updateById(newEmployee);
//        }
//        if (flag) {
//            employeeinfoChangeLogService.addEmployeeInfoChangeLog(oldEmp,newEmployee);
//            return ReturnResult.success().message("修改成功");
//        }
//        return ReturnResult.failed().message("修改失败");
//    }
//
//
//    /**
//     * 根据id删除员工
//     * @param employeeId
//     * @return
//     */
//    @RequiresPermissions("service:em:remove")
//    @PostMapping("removeEmployeeById/{employeeId}")
//    @Log("删除员工信息")
//    public ReturnResult deleteEmployeeById(@PathVariable Integer employeeId){
//        if (employeeId==null&&employeeService.getById(employeeId)==null){
//            throw new MyException(ResultCode.NOTQUERIED,"该员工不存在");
//        }
//        int row =employeeService.removeEmployeeById(employeeId);
//        if (row>0){
//            return ReturnResult.success().message("删除成功");
//        }
//        return ReturnResult.failed().message("删除失败");
//    }
//
//    /**
//     * 注销员工账号
//     * @param employeeId
//     * @return
//     */
//    @RequiresPermissions("service:em:revoked")
//    @DeleteMapping("revokedEmployeeById/{employeeId}")
//    @Log("注销员工账号")
//    public ReturnResult revokedEmployeeById(@PathVariable Integer employeeId){
//        if (employeeId==null && employeeService.getById(employeeId) == null){
//            throw new MyException(ResultCode.NOTQUERIED,"该员工不存在");
//        }
//
//        int row =employeeService.revokedEmployeeById(employeeId);
//        if (row>0){
//            return ReturnResult.success().message("账号注销成功");
//
//        }
//        return ReturnResult.failed().message("账号注销失败");
//    }
//
//
//    /**
//     * 分页条件查询员工信息
//     * @param current
//     * @param size
//     * @param employeeVo
//     * @return
//     */
//    @RequiresPermissions("service:em:list")
//    @PostMapping("getEmployeePage/{current}/{size}")
//    public ReturnResult getEmployeePage(@PathVariable long current, @PathVariable long size, @RequestBody(required = false)employeeCheckVo employeeVo){
//        if (current==0||size==0){
//            throw new MyException(ResultCode.PARAMETER,"参数为空");
//        }
//
//        Page<employeeShowInfo> page =new Page<>(current,size);
//        IPage<employeeShowInfo> employeePage = employeeService.getEmployeePage(employeeVo,page);
//        return ReturnResult.success().data("employee",employeePage.getRecords()).data("total",employeePage.getTotal());
//    }
//
//    /**
//     * 重置密码
//     * @param eid
//     * @return
//     */
//    @RequiresPermissions("service:em:reset")
//    @GetMapping("resetPassword/{eid}")
//    @Log("重置登录密码")
//    public ReturnResult resetPassword(@PathVariable Integer eid){
//        if (eid==null){
//            throw new MyException(ResultCode.PARAMETER,"缺少参数");
//        }
//        int row =employeeService.resetPassword(eid);
//        if (row>0){
//            return ReturnResult.success().message("重置密码成功");
//        }
//        return ReturnResult.failed().message("重置密码失败");
//    }
//
//    /**
//     * 挂失
//     * @param eid
//     * @return
//     */
//    @RequiresPermissions("service:em:loss")
//    @GetMapping("LossCard/{eid}")
//    @Log("工作证挂失")
//    @AfterReturning()
//    public ReturnResult lossCard(@PathVariable Integer eid){
//        if(eid==null){
//            throw new MyException(ResultCode.PARAMETER,"缺少参数");
//        }
//        InfoEmployee employee = employeeService.getById(eid);
//        if (StringUtils.isNull(employee)){
//            throw new MyException(ResultCode.NOTQUERIED,"该员工不存在");
//        }
//        if (employee.getValid()!=0){
//            throw new MyException(ResultCode.FAIL,"该账号状态异常，无法挂失");
//        }
//
//        int b = employeeService.lossCard(employee);
//        if (b>0){
//            return ReturnResult.success().message("更新状态成功");
//        }
//        return ReturnResult.failed().message("更新状态失败");
//    }
//
//    /**
//     * 根据eid获取该用户挂失信息
//     * @param eid
//     * @return
//     */
//    @GetMapping("getLossMessage/{eid}")
//    public ReturnResult getLossMessage(@PathVariable Integer eid){
//        if(eid==null){
//            throw new MyException(ResultCode.PARAMETER,"缺少参数");
//        }
//
//        employeeShowInfo employeeInfoFront = employeeService.getLossMessage(eid);
//        return ReturnResult.success().message("查询成功").data("lossMessage",employeeInfoFront);
//    }
//
//    /**
//     * 发卡/补卡
//     * @param eId
//     * @param macId
//     * @return
//     */
//    @RequiresPermissions("service:em:product")
//    @GetMapping("productCard/{eId}/{macId}")
//    public ReturnResult mendCard(@PathVariable @NotNull Integer eId, @PathVariable @NotNull String macId){
//        int flag = employeeService.productCard(eId,macId);
//        if (flag>0){
//            return ReturnResult.success().message("制卡成功");
//        }
//        return ReturnResult.failed().message("制卡失败");
//    }
//
//    /**
//     * 退款
//     * @param eId
//     * @return
//     */
//    @RequiresPermissions("service:em:refund")
//    @Log("职工退款")
//    @GetMapping("refund/{eId}/{value}")
//    public ReturnResult refund(@PathVariable @NotNull Integer eId,@PathVariable @Min(value = 1,message = "退款金额不可小于1") Integer value){
//        InfoEmployee employee = employeeService.getById(eId);
//        if (employee==null){
//            return ReturnResult.failed().message("没有查询到职工信息");
//        }
//        //余额
//        Double cardBalance = employee.getCardBalance();
//        if (cardBalance<value){
//            throw new MyException(ResultCode.FAIL,"余额不足");
//        }
//
//        employee.setCardBalance(cardBalance-value);
//        boolean b = employeeService.updateById(employee);
//        if (b){
//            return ReturnResult.success().message("退款成功:"+value+"元");
//        }
//        return ReturnResult.failed().message("退款失败");
//    }
//
//    /**
//     * 职工修改小程序角色
//     * @param employeeModificationMiniProgramPerviewDTO
//     * @return
//     */
//  @PostMapping("employeeModificationMiniProgramPerview")
//  @Log("职工修改小程序角色")
//  public ReturnResult employeeModificationMiniProgramPerview(@RequestBody(required = false) EmployeeModificationMiniProgramPerviewDTO employeeModificationMiniProgramPerviewDTO) {
//        if (employeeService.getById(employeeModificationMiniProgramPerviewDTO.getEmployeeId())==null){
//            throw new MyException(ResultCode.NOTQUERIED,"该员工不存在");
//        }
//      Integer b = employeeService.employeeModificationMiniProgramPerview(employeeModificationMiniProgramPerviewDTO);
//      if (b>0){
//          return ReturnResult.success().message("修改成功");
//      }
//      return ReturnResult.failed().message("修改失败");
//
//    }
//
//    /**
//     * 查询职工余额
//     * @param uid
//     * @return
//     */
//    @PostMapping("getBalance/{uid}")
//    public ReturnResult getBalanceById(@PathVariable int uid){
//        InfoEmployee byId = employeeService.getById(uid);
//        if (byId!=null){
//            return ReturnResult.success().data("balance",byId.getCardBalance()).message("查询成功");
//        }
//        return ReturnResult.failed().message("查询失败");
//    }
//}
